package com.aba.hos.doc.service;



import com.aba.hos.doc.entity.Doctor;
import com.aba.hos.doc.entity.Patient;
import com.aba.hos.doc.entity.Ward;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 医院服务类，提供病人、医生和病房的管理功能
 */
public class HospitalService {
    private final List<Patient> patients;
    private final List<Doctor> doctors;
    private final List<Ward> wards;

    public HospitalService() {
        this.patients = new ArrayList<>();
        this.doctors = new ArrayList<>();
        this.wards = new ArrayList<>();
    }

    // 病人管理
    public void addPatient(Patient patient) {
        patients.add(patient);
    }

    public void dischargePatient(int patientId) {
//        Patient patient = findPatientById(patientId);
        Patient patient = null;
        if (patient != null && !patient.isDischarged()) {
            patient.discharge();
            System.out.println("病人 " + patient.getName() + " 已出院");
        } else {
            System.out.println("未找到该病人或病人已出院");
        }
    }

    public Patient findPatientById(String patientId) {

//        List<Patient> patientList = patients.stream()
//                .filter(p -> p.getDocId() == patientId)
//                .collect(Collectors.toList());
        return null;
    }

    public List<Patient> getAllPatients() {
        return new ArrayList<>(patients);
    }

    public List<Patient> getActivePatients() {
        return patients.stream()
                .filter(p -> !p.isDischarged())
                .collect(Collectors.toList());
    }

    // 医生管理
    public void addDoctor(Doctor doctor) {
        doctors.add(doctor);
    }

    public Doctor findDoctorById(int doctorId) {
        return doctors.stream()
                .filter(d -> d.getId() == doctorId)
                .findFirst()
                .orElse(null);
    }

    public List<Doctor> getAllDoctors() {
        return new ArrayList<>(doctors);
    }

    public void assignDoctorToPatient(int doctorId, int patientId) {
        Doctor doctor = findDoctorById(doctorId);
//        Patient patient = findPatientById(patientId);
        Patient patient = null;

        if (doctor == null) {
            System.out.println("未找到该医生");
            return;
        }

        if (patient == null) {
//            System.out.println("未找到该病人");
            return;
        }

        if (patient.isDischarged()) {
            System.out.println("病人已出院，无法分配医生");
            return;
        }

        doctor.addPatient(patient);
        System.out.println("医生 " + doctor.getName() + " 已分配给病人 " + patient.getName());
    }

    // 病房管理
    public void addWard(Ward ward) {
        wards.add(ward);
    }

    public Ward findWardByNumber(int wardNumber) {
        return wards.stream()
                .filter(w -> w.getWardNumber() == wardNumber)
                .findFirst()
                .orElse(null);
    }

    public List<Ward> getAllWards() {
        return new ArrayList<>(wards);
    }

    public List<Ward> getAvailableWards() {
        return wards.stream()
                .filter(w -> w.getAvailableBeds() > 0)
                .collect(Collectors.toList());
    }

    public boolean assignPatientToWard(int patientId, int wardNumber) {
        Ward ward = findWardByNumber(wardNumber);
        Patient patient = null;
        if (patient == null) {
            return false;
        }

        if (ward == null) {
            System.out.println("未找到该病房");
            return false;
        }

        if (patient.isDischarged()) {
            System.out.println("病人已出院，无法分配病房");
            return false;
        }

        if (ward.addPatient(patient)) {
            System.out.println("病人 " + patient.getName() + " 已分配到病房 " + ward.getWardNumber());
            return true;
        } else {
            System.out.println("病房已满，无法分配");
            return false;
        }
    }

    // 统计功能
    public int getTotalPatients() {
        return patients.size();
    }

    public int getActivePatientCount() {
        return (int) patients.stream()
                .filter(p -> !p.isDischarged())
                .count();
    }

    public int getTotalDoctors() {
        return doctors.size();
    }

    public int getTotalWards() {
        return wards.size();
    }

    public int getAvailableBeds() {
        return wards.stream()
                .mapToInt(Ward::getAvailableBeds)
                .sum();
    }

    public Map<String, Integer> getWardOccupancy() {
        Map<String, Integer> occupancy = new HashMap<>();
        for (Ward ward : wards) {
            occupancy.put("病房 " + ward.getWardNumber(), ward.getPatients().size());
        }
        return occupancy;
    }

    public Map<String, Integer> getDoctorPatientCount() {
        Map<String, Integer> countMap = new HashMap<>();
        for (Doctor doctor : doctors) {
            countMap.put(doctor.getName(), doctor.getPatients().size());
        }
        return countMap;
    }
}    